//
//  HomeTableViewController.m
//  weibo110
//
//  Created by 臧松 on 15/12/25.
//  Copyright © 2015年 臧松. All rights reserved.
//
#define StatusCellNormalId @"StatusCellNormalId"
#define StatusCellRetweetedId @"StatusCellRetweetedId"
#import "HomeTableViewController.h"
#import "NetworkTools.h"
#import "UserAccountViewModel.h"
#import "UserAccount.h"
#import "Status.h"
#import "StatusListViewModel.h"
#import "SVProgressHUD.h"
#import "StatusViewModel.h"
#import "StatusCell.h"
#import "StatusRetweetedCell.h"
#import "StatusNormalCell.h"
#import "WBRefreshControl.h"
@interface HomeTableViewController ()

@property (nonatomic,strong) StatusListViewModel * statusListModel;
@property (nonatomic,strong) UIActivityIndicatorView * pullupView;

@end

@implementation HomeTableViewController

-(StatusListViewModel *)statusListModel{
    if (_statusListModel == nil) {
        _statusListModel = [[StatusListViewModel alloc]init];
    }
    return _statusListModel;
}
-(UIActivityIndicatorView *)pullupView{
    
    if (_pullupView == nil) {
        _pullupView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _pullupView.color = [UIColor lightGrayColor];
    }
    return _pullupView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (![UserAccountViewModel shareUserAccount].userLogon) {
        
        [self.visitorView initWithImageName:nil title:@"关注一些人，回这里看看有什么惊喜"];
    }
    
    [self loadData];
    
    [self.tableView registerClass:[StatusRetweetedCell class] forCellReuseIdentifier:StatusCellRetweetedId];
    
    [self.tableView registerClass:[StatusNormalCell class] forCellReuseIdentifier:StatusCellNormalId];
    self.tableView.dataSource = self;
    // 预估行高 - 需要尽量准确
    self.tableView.estimatedRowHeight = 400;
   
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 下拉刷新控件默认没有
    self.refreshControl = [[WBRefreshControl alloc]init];
    
    [self.refreshControl beginRefreshing];
    
    // 添加监听方法
    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    // 上拉刷新控件
    self.tableView.tableFooterView = self.pullupView;
}

-(void)loadData{
    [self.statusListModel loadStatusDatafinished:^(BOOL isSuccessed) {
        
        [self.refreshControl endRefreshing];
        if (!isSuccessed) {
            [SVProgressHUD showErrorWithStatus:@"加载数据错误,请稍后再试"];
            return ;
        }
        // 刷新数据
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.statusListModel.statusList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    StatusViewModel *vm = self.statusListModel.statusList[indexPath.row];
    
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:vm.cellId forIndexPath:indexPath];
    
    cell.statusViewModel = vm;
    // 判断是否是最后一条微博
    if ((indexPath.row == self.statusListModel.statusList.count - 1)&& !self.pullupView.isAnimating) {
        // 开始动画
        [self.pullupView startAnimating];
        NSLog(@"上拉刷新");
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取出模型
    StatusViewModel *statusViewModel = self.statusListModel.statusList[indexPath.row];
    return statusViewModel.rowHeight;
}

@end
